package study.nababy.recyclerview.first.adapter;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import study.nababy.recyclerview.R;
import study.nababy.recyclerview.first.adapter.CommonAdapter;
import study.nababy.recyclerview.first.adapter.ListRecycleAdapter;

public class ListRecycleActivity extends AppCompatActivity {

    private RecyclerView list_recycle_view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_recycler);
        initViews();
    }

    //初始化当前布局
    private void initViews() {
        list_recycle_view = (RecyclerView) findViewById(R.id.list_recycle_view);
        initRecycle();
    }

    /**
     *设置RecyclerView参数
     */
    private void initRecycle() {
        // 如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        list_recycle_view.setHasFixedSize(true);

        // 确定每个item的排列方式
        LinearLayoutManager layoutManager = new LinearLayoutManager(this) {
            @Override
            public RecyclerView.LayoutParams generateDefaultLayoutParams() {
                // 这里要复写一下，因为默认宽高都是wrap_content
                // 这个不复写，你点击的背景色就只充满你的内容
                return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
            }
        };
        //设置RecyclerView为水平状态还是垂直状态
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        list_recycle_view.setLayoutManager(layoutManager);

        CommonAdapter mAdapter = new ListRecycleAdapter(getApplicationContext(), R.layout.list_recycle_view, 30);
        mAdapter.setOnItemClickListener(new CommonAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(getApplicationContext(), "Item " + position + " click"
                        , Toast.LENGTH_SHORT).show();
            }
        });
        list_recycle_view.setAdapter(mAdapter);

    }
}
